package com.ruoyi.web.controller.system;

import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.system.domain.StbCompany;
import com.ruoyi.system.domain.StbProduct;
import com.ruoyi.system.service.IStbCompanyService;
import com.ruoyi.system.service.IStbProductService;

/**
 * 产品Controller
 * 
 * @author lxz
 * @date 2020-02-08
 */
@Controller
@RequestMapping("/system/product")
public class StbProductController extends BaseController
{
    private String prefix = "system/product";

    @Autowired
    private IStbProductService stbProductService;
    
    @Autowired
	private IStbCompanyService stbCompanyService;

    @RequiresPermissions("system:product:view")
    @GetMapping()
    public String product(ModelMap mmap)
    {
    	StbCompany stbCompany = new  StbCompany();
    	List<StbCompany> list = stbCompanyService.selectStbCompanyList(stbCompany);
    	mmap.put("companyList",list);
        return prefix + "/product";
    }
    
    /*
	 * 查询产品库产品名称和所属企业名称  compname:产品名称 propname:
	 */
    @PostMapping("/namelist")
    @ResponseBody
    public List<Map<String,Object>> selectProductName(){
    	StbProduct stbProduct = new StbProduct();
    	List<Map<String, Object>> pronameList = stbProductService.selectProductName(stbProduct);
    	return pronameList;
    }

    /**
     * 查询产品列表
     */
    @RequiresPermissions("system:product:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(StbProduct stbProduct)
    {
        startPage();
        List<StbProduct> list = stbProductService.selectStbProductList(stbProduct);
        return getDataTable(list);
    }

    /**
     * 导出产品列表
     */
    @RequiresPermissions("system:product:export")
    @Log(title = "产品", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(StbProduct stbProduct)
    {
        List<StbProduct> list = stbProductService.selectStbProductList(stbProduct);
        ExcelUtil<StbProduct> util = new ExcelUtil<StbProduct>(StbProduct.class);
        return util.exportExcel(list, "product");
    }

    /**
     * 新增产品
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
    	StbCompany stbCompany = new  StbCompany();
    	List<StbCompany> list = stbCompanyService.selectStbCompanyList(stbCompany);
    	mmap.put("companyList",list);
        return prefix + "/add";
    }

    /**
     * 新增保存产品
     */
    @RequiresPermissions("system:product:add")
    @Log(title = "产品", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(StbProduct stbProduct)
    {
    	stbProduct.setCreateBy(ShiroUtils.getLoginName());
        return toAjax(stbProductService.insertStbProduct(stbProduct));
    }

    /**
     * 修改产品
     */
    @GetMapping("/edit/{prodId}")
    public String edit(@PathVariable("prodId") Long prodId, ModelMap mmap)
    {
    	StbCompany stbCompany = new  StbCompany();
    	List<StbCompany> list = stbCompanyService.selectStbCompanyList(stbCompany);
    	mmap.put("companyList",list);
        StbProduct stbProduct = stbProductService.selectStbProductById(prodId);
        mmap.put("stbProduct", stbProduct);
        return prefix + "/edit";
    }

    /**
     * 修改保存产品
     */
    @RequiresPermissions("system:product:edit")
    @Log(title = "产品", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(StbProduct stbProduct)
    {
    	stbProduct.setUpdateBy(ShiroUtils.getLoginName());
        return toAjax(stbProductService.updateStbProduct(stbProduct));
    }

    /**
     * 删除产品
     */
    @RequiresPermissions("system:product:remove")
    @Log(title = "产品", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(stbProductService.deleteStbProductByIds(ids));
    }
}
